//
//  MenuButtonView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/3.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "MenuButtonView.h"


@interface MenuButtonView ()
/**  scrollView  */
@property (strong, nonatomic) UIScrollView  *scrollView;
/**  标题数组  */
@property (strong, nonatomic) NSArray  *titles;
/**  红色下划线  */
@property (strong, nonatomic) UIView  *redUnderLine;
/**  上一个按钮  */
@property (strong, nonatomic) UIButton  *lastButton;


@end
@implementation MenuButtonView

- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titles = titles;
        [self addSubview:self.scrollView];
        
        /**  红色下划线  */
        self.redUnderLine = [[UIView alloc] initWithFrame:CGRectMake(15 * [UIScreen zj_screenWScale], CGRectGetMaxY(self.scrollView.frame), 0, 2 )];
        self.redUnderLine.backgroundColor = [UIColor themeColor];
        [self addSubview:self.redUnderLine];
        
       self.underLine = [UIView new];
        self.underLine.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:self.underLine];
        self.underLine.sd_layout.leftEqualToView(self).bottomEqualToView(self).widthIs([UIScreen zj_screenWith]).heightIs(1.5 * [UIScreen zj_screenHScale]);
        
        
        [self addButton];
    }
    return self;
}

/**  添加按钮方法  */
- (void)addButton{
    for (int i = 0; i < self.titles.count; i ++) {
        /**  宽度  */
        CGFloat width = [CalculateLabelHeight CalculateLabelWidthWithString:self.titles[i] font:16 * [UIScreen zj_textFontScale] height:16 * [UIScreen zj_screenHScale]].width;
        
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake( CGRectGetMaxX(self.lastButton.frame) + 20 * [UIScreen zj_screenWScale], 0, width, 43 * [UIScreen zj_screenHScale])];
        [button setTitle:self.titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
        button.tag = i;
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
        
        self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(button.frame) + 10 * [UIScreen zj_screenWScale], self.scrollView.height);
        
        if (i == 0) {
            button.frame = CGRectMake(15 * [UIScreen zj_screenWScale], 0, width, 43 * [UIScreen zj_screenHScale]);
            CGRect frame = self.redUnderLine.frame;
            frame.size.width = width;
            self.redUnderLine.frame = frame;
            
           
        }
        self.lastButton = button;
    }

}


/**  按钮的点击事件  */
- (void)clickButton:(UIButton *)sender{
   [UIView animateWithDuration:0.4 animations:^{
       CGRect frame = self.redUnderLine.frame;
       frame.size.width = sender.width;
       frame.origin.x = sender.x;
       self.redUnderLine.frame = frame;
   }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuButtonWithView:buttonTitle:tag:)]) {
        [self.delegate menuButtonWithView:self buttonTitle:self.titles[sender.tag] tag:sender.tag];
    }
    

}


#pragma mark - getter
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.width, 43 * [UIScreen zj_screenHScale])];
        _scrollView.contentOffset = CGPointMake(0, 0);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        
    }
    return _scrollView;
    
}

@end
